toConfigure()

function toConfigure() {
    $.ajax({
        url: root.sroreinter.getSubDomainList,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNumber": 1,
            "pageSize": 10,
            "name": $("#name").val(),
            "subDomain": $("#subDomain1").val(),

        }),
        success: function(data) {
            toConfigurelist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    $.ajax({
                        url: root.sroreinter.getSubDomainList,
                        cache: false, //禁用缓存
                        type: "POST",
                        dataType: 'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNumber": api.getCurrent(),
                            "pageSize": 10,
                            "name": $("#name").val(),
                            "subDomain": $("#subDomain1").val(),

                        }),
                        success: function(result) {
                            toConfigurelist(result.data);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
var dataSource = new Array();
var table; //table对象
dataSource = [];

function toConfigurelist(data, index) {
    dataSource.splice(0, dataSource.length); //清空数组
    if (data.data != null && data.data != '') {
        var datashu = data.pageStart + 1;
        $.each(data.data, function(i, field) {
            var datashus = datashu++
                var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.id);
            rowData.push(field.name);
            rowData.push(field.subDomain);
            rowData.push(field.note);
            rowData.push(field.createTimeStr);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#toConfiguretable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#toConfiguretable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [{
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },

                {
                    "render": function(data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = ""
                        if (row[3] != "") {
                            val = 'https://' + row[3] + '.fsyuncai.com'
                        } else {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },

                {
                    "render": function(data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },

                {
                    "render": function(data, type, row, meta) {
                        var val = '<span class=" "><a onclick="editConfiguration(\'' + row[1] + '\')" style="margin-right: 14px">编辑</a></span><span class=" "><a  style="margin-right: 14px" onclick="deletes(\'' + row[1] + '\')">删除</a> </span>'

                        return val;
                    },
                    "targets": 5
                },

            ]
        });
        //权限控制函数
        power()
    };
}
//删除
function deletes(id) {
    var curInd = layer.confirm(
        '<span>确定删除吗？</span>' + '<br/>', { title: '提示', btn: ['确定', '取消'] },
        function(index) {
            $.ajax({
                url: root.sroreinter.delSubDomain,
                cache: false, //禁用缓存
                type: "POST",
                dataType: 'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "id": id,
                }),
                success: function(data) {
                    if (data.errorCode == 0) {
                        $.Huimodalalert('删除成功', 2000);
                        toConfigure()

                    } else {

                        $.Huimodalalert(data.errorMessage, 2000);

                    }
                },
                error: function() {
                    $.Huimodalalert('失败！', 2000);
                }
            })
            layer.close(curInd);

        },
        function() {
            layer.close(curInd);

        });

}
//添加
function newConfiguration() {
    $("#newConfiguration").modal("show");
    $(".addressTitle").html("新增");
}
//编辑
function editConfiguration(id) {
    $("#newConfiguration").modal("show");
    $(".addressTitle").html("编辑");
    $.ajax({
        url: root.sroreinter.toEditSubDomain,
        cache: false, //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id": id,
        }),
        success: function(data) {
            $("#name2").val(data.data.name)
            $("#subDomain").val(data.data.subDomain)
            $("#secondarysrc").attr("src", data.data.imgUrl)
            $("#remark").val(data.data.note)
            $("#newConfigurationid").val(data.data.id)
            $("#bookMark").val(data.data.bookMark)
            $("#loginMsg").val(data.data.loginMsg)
            $("#secondarysrc2").attr("src", data.data.pageTailUrl)
            $("input[name='color']").val(data.data.color);
            $("#headPortrait-img").attr("src", data.data.headPortrait);
            $("#headPortrait").val(data.data.headPortrait);
            $("#xfs-img").attr("src", data.data.headlineImage);
            $("#headlines-img").val(data.data.headlineImage);
        },
        error: function() {
            $.Huimodalalert('失败！', 2000);
        }
    })

}
//取消
function close_model() {
    $("#newConfiguration").modal("hide");
    $("#name2").val("")
    $("#bookMark").val("")
    $("#subDomain").val("")
    $("#remark").val("")
    $("#secondarysrc").attr("src", "")
    $("#newConfigurationid").val()
    $("#xfs-img").attr("src", "")
    $("#headlines-img").val()
}


//确定
function determine() {
    // console.log($("#headlines-img").val())
    if ($("#name2").val() == "") {
        $.Huimodalalert('名称不能为空', 2000);
        return false;
    }
    if ($("#subDomain").val() == "") {
        $.Huimodalalert('链接不能为空', 2000);
        return false;
    }
    // if ($("#secondarysrc").attr("src") == "") {
    //     $.Huimodalalert('logo不能为空', 2000);
    //     return false;
    // }
    if ($("#remark").val() == "") {
        $.Huimodalalert('备注不能为空', 2000);
        return false;
    }
    var colorVal = $("input[name='color']").val();
    if (!colorVal) {
        $.Huimodalalert('登录语颜色不能为空', 2000);
        return false;
    }


    if ($(".addressTitle").html() == "编辑") {
        $.ajax({
            url: root.sroreinter.updateSubDomain,
            cache: false, //禁用缓存
            type: "POST",
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({
                "name": $("#name2").val(),
                "bookMark": $("#bookMark").val(),
                "subDomain": $("#subDomain").val().trim().toLowerCase(),
                "imgUrl": $("#secondarysrc").attr("src"),
                "note": $("#remark").val(),
                "id": $("#newConfigurationid").val(),
                "loginMsg": $("#loginMsg").val(),
                "pageTailUrl": $("#secondarysrc2").attr("src"),
                "color": colorVal,
                "headPortrait": $("#headPortrait").val(),
                "headlineImage": $("#headlines-img").val()
            }),
            success: function(data) {
                if (data.errorCode == 0) {
                    $.Huimodalalert('编辑成功', 2000);
                    $("#newConfiguration").modal("hide");
                    $("#name2").val("")
                    $("#bookMark").val("")
                    $("#subDomain").val("")
                    $("#remark").val("")
                    $("#secondarysrc").attr("src", "")
                    $("#newConfigurationid").val("")
                    $("#loginMsg").val("")
                    $("#secondarysrc2").attr("src", "")
                    $("#xfs-img").attr("src", "")
                    $("#headlines-img").val("")
                    toConfigure()

                } else {

                    $.Huimodalalert(data.errorMessage, 2000);

                }
            },
            error: function() {
                $.Huimodalalert('失败！', 2000);
            }
        })
    } else {

        $.ajax({
            url: root.sroreinter.addSubDomain,
            cache: false, //禁用缓存
            type: "POST",
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({
                "name": $("#name2").val(),
                "bookMark": $("#bookMark").val(),
                "subDomain": $("#subDomain").val().trim().toLowerCase(),
                "imgUrl": $("#secondarysrc").attr("src"),
                "note": $("#remark").val(),
                "loginMsg": $("#loginMsg").val(),
                "pageTailUrl": $("#secondarysrc2").attr("src"),
                "color": colorVal,
                "headPortrait": $("#headPortrait").val(),
                "headlineImage": $("#headlines-img").val()
            }),
            success: function(data) {
                if (data.errorCode == 0) {

                    $.Huimodalalert('添加成功', 2000);


                    $("#newConfiguration").modal("hide");
                    $("#name2").val("")
                    $("#bookMark").val("")
                    $("#subDomain").val("")
                    $("#remark").val("")
                    $("#secondarysrc").attr("src", "")
                    $("#newConfigurationid").val("")
                    $("#loginMsg").val("")
                    $("#secondarysrc2").attr("src", "")
                    $("#xfs-img").attr("src", "")
                    $("#headlines-img").val("")
                    toConfigure()

                } else {

                    $.Huimodalalert(data.errorMessage, 2000);

                }
            },
            error: function() {
                $.Huimodalalert('失败！', 2000);
            }
        })
    }


}
//formdata上传图片公用方法
//触发事件
var ipcuu = ""
var pathss = ""

function addBtn(cen, ir) {
    ipcuu = ir
    $(cen).prev().prev().trigger('click');
}
var formData = new FormData();
// 提交上传图片
function getupfilerul(can, e) {
    var formData = new FormData();
    formData.append('file', $(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false, //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function(data) {
            pathss = data[0].fullPath
            for (var i = 0; i < data.length; i++) {
                $(can).prev().val(data[0].reletivePath)
                $(can).prev().text(data[0].reletivePath)
                $("#secondarysrc").attr("src", data[0].fullPath)
            }
        }
    })
}
// 上传图片返现
function putDataToPage(e, can) {
    /*verificationPicFileType(can)
    verificationPicFileSize(can)*/
    // verificationPicFileMeasure(can)
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }


        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        console.log(e.path)
        console.log(can)
            /* freader.onload = function (e) {
                /!* $(can).parent().prev().children().attr("src", e.target.result);*!/
                 getupfilerul(can,e)
             }*/
        freader.onload = function(e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src = data;
            img.onload = function() {
                var width = img.width;
                var height = img.height;
                if (width != 220 | height != 65) {
                    $.Huimodalalert('图片尺寸限制为：220px*65px', 2000);
                    file.value = "";
                    return file;
                } else {
                    getupfilerul(can, e)
                }

            };

        }
    }
    formData.append('file', $(can)[0].files[0]);
}
//页尾图片
function addBtn2(cen, ir) {
    $(cen).prev().prev().trigger('click');
}
var formData = new FormData();
// 提交上传图片
function getupfilerul2(can, e) {
    var formData = new FormData();
    formData.append('file', $(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false, //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function(data) {
            pathss = data[0].fullPath
            for (var i = 0; i < data.length; i++) {
                $(can).prev().val(data[0].reletivePath)
                $(can).prev().text(data[0].reletivePath)
                $("#secondarysrc2").attr("src", data[0].fullPath)
            }
        }
    })
}
// 上传图片返现
function putDataToPage2(e, can) {
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }


        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        console.log(e.path)
        console.log(can)
        freader.onload = function(e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src = data;
            img.onload = function() {
                var width = img.width;
                var height = img.height;
                if (width != 1200 | height != 260) {
                    $.Huimodalalert('图片尺寸限制为：1200px*260px', 2000);
                    file.value = "";
                    return file;
                } else {
                    getupfilerul2(can, e)
                }

            };

        }
    }
    formData.append('file', $(can)[0].files[0]);
}
// 图片类型验证
function verificationPicFileType(file) {
    var flag = false;
    var fileTypes = [".jpg", ".png", ".JPG", ".PNG"];
    var filePath = file.value;
    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
    if (filePath) {
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag = true;
                break;
            }
        }
        if (!isNext) {
            $.Huimodalalert('不接受此类型，请上传jpg或png格式', 2000);
            file.value = "";
            flag = false;
        }
    }
    return flag;
}
//图片大小验证
function verificationPicFileSize(file) {
    var flag = false;
    var fileSize = 0;
    var fileMaxSize = Number(200);
    var filePath = file.value;
    if (filePath) {
        fileSize = file.files[0].size;
        var size = fileSize / Number(1024);
        if (size > fileMaxSize) {
            $.Huimodalalert('图片大小不可超过200KB', 2000);
            file.value = "";
            flag = false;
        } else {
            flag = true;
        }
    }
    return flag;
}
//图片尺寸验证
function verificationPicFileMeasure(file) {
    var flag = false;
    var filePath = file.value;
    if (filePath) {
        //读取图片数据
        var filePic = file.files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src = data;
            img.onload = function() {
                var width = img.width;
                var height = img.height;
                if (width != 175 | height != 55) {
                    $.Huimodalalert('图片尺寸为175*55px', 2000);
                    $("#secondarysrc").attr("src", "")
                    file.value = "";
                    flag = false;
                } else {
                    flag = true;
                }

            };

        };
        reader.readAsDataURL(filePic);
    } else {
        flag = false;
    }
    return flag;
}


// 上传头像
function uploadPortrait(e, can) {
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }


        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        console.log(e.path)
        console.log(can)
        freader.onload = function(e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src = data;
            img.onload = function() {
                var width = img.width;
                var height = img.height;
                if (width != 230 | height != 119) {
                    $.Huimodalalert('图片尺寸限制为：230px*119px', 2000);
                    file.value = "";
                    return file;
                } else {
                    getPortraitUrl(can, e)
                }
                //getPortraitUrl(can,e)
            };

        }
    }
    formData.append('headPortrait', $(can)[0].files[0]);
}

// 提交上传图片
function getPortraitUrl(can, e) {
    var formData = new FormData();
    formData.append('file', $(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false, //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function(data) {
            pathss = data[0].fullPath
            for (var i = 0; i < data.length; i++) {
                $(can).prev().val(data[0].reletivePath)
                $(can).prev().text(data[0].reletivePath)
                $("#headPortrait-img").attr("src", data[0].fullPath)
                $("#headPortrait").val(data[0].fullPath);
            }
        }
    })
}

//页尾图片
function addBtnUploadPortrait(cen, ir) {
    $(".headPortrait").trigger('click');
}
// 鑫方盛头条图片
function addHeadlinesImg(cen, ir) {
    $(".headlines-img").trigger('click');
}
// 上传头像
function uploadHeadlines(e, can) {
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }


        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        console.log(e.path)
        console.log(can)
        freader.onload = function(e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src = data;
            img.onload = function() {
                var width = img.width;
                var height = img.height;
                if (width != 152 | height != 28) {
                    $.Huimodalalert('图片尺寸限制为：152px*28px', 2000);
                    file.value = "";
                    return file;
                } else {
                    getHeadlinesUrl(can, e)
                }
                //getPortraitUrl(can,e)
            };

        }
    }
    formData.append('headlines', $(can)[0].files[0]);
}
// 提交上传图片
function getHeadlinesUrl(can, e) {
    var formData = new FormData();
    formData.append('file', $(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false, //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        data: formData,
        success: function(data) {
            pathss = data[0].fullPath
            for (var i = 0; i < data.length; i++) {
                $(can).prev().val(data[0].reletivePath)
                $(can).prev().text(data[0].reletivePath)
                $("#xfs-img").attr("src", data[0].fullPath)
                $("#headlines-img").val(data[0].fullPath);
            }
        }
    })
}